﻿using AutoMapper;
using PowerHub.MES.DTO.BasicData.DTO;
using PowerHub.MES.DTO.BasicData.QueryDto;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities;
using PowerHub.MES.Entities.BasicData;
using PowerHub.MES.IRepository.IBasic;
using PowerHub.MES.IServices.IBasic;
using PowerHub.MES.Jwt;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace PowerHub.MES.Services.Basic
{
    /// <summary>
    /// 菜单接口实现层
    /// </summary>
    public class BasicData_MenuInfoServices : BaseServices<BasicData_MenuInfo>, IBasicData_MenuInfoServices
    {
        private IBasicData_MenuInfoRepository _ibasicData_MenuInfoRepository;
        private IBasicData_Roleinfo_MenuInfoRepository _basicData_Roleinfo_MenuInfoRepository;

        public UserServices _userServices;
        private IMapper _mapper;
        private static Random random = new Random();

        public BasicData_MenuInfoServices(PowerHubMesContext powerHubMesContext, IBasicData_MenuInfoRepository ibasicData_MenuInfoRepository,
            UserServices userServices, IMapper mapper, IBasicData_Roleinfo_MenuInfoRepository basicData_Roleinfo_MenuInfoRepository) : base(powerHubMesContext)
        {
            _ibasicData_MenuInfoRepository = ibasicData_MenuInfoRepository;
            _basicData_Roleinfo_MenuInfoRepository = basicData_Roleinfo_MenuInfoRepository;
            _userServices = userServices;
            _mapper = mapper;
        }



        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<BasicData_MenuInfoDTOs>>> GetBasicData_MenuInfoListAsync(QueryBasicData_MenuInfoDTO query)
        {
            var result = new OperationPageResult<List<BasicData_MenuInfoDTOs>>();

            //获取菜单集合
            var basicData_MenuInfos = await GetEntityListAsync(m => m.MenuInfoName.Contains(query.MenuInfoName ?? string.Empty) && m.IsDel == 0);

            //取出一级菜单
            List<BasicData_MenuInfoDTOs> menuInfoList = basicData_MenuInfos.Where(m => m.ParentId == null || m.ParentId == "").Select(m => new BasicData_MenuInfoDTOs(m)).OrderBy(m => m.Sort).ToList();

            GetChild(menuInfoList, basicData_MenuInfos);

            result.Data = menuInfoList;
            return result;
        }


        /// <summary>
        /// 根据角色id获取已选菜单集合
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<List<string>>> GetBasicData_RoleinfoById_MenuInfoListAsync(QueryBasicData_Roleinfo_MenuInfoDTO query)
        {
            //根据角色id获取已选菜单集合
            List<string> menuInfos = await _basicData_Roleinfo_MenuInfoRepository.GetBasicData_Roleinfo_MenuInfoPageListAsync(query);

            //获取菜单集合
            var basicData_MenuInfos = await GetEntityListAsync(m => menuInfos.Contains(m.Id) && m.IsDel == 0);


            return basicData_MenuInfos.Where(m => m.ParentId != null && m.ParentId != "").Select(m => m.Id).ToList();
        }


        /// <summary>
        /// 递归获取子菜单项
        /// </summary>
        /// <param name="menuInfoList"></param>
        /// <param name="menuInfosss"></param>
        private void GetChild(List<BasicData_MenuInfoDTOs> menuInfoList, List<BasicData_MenuInfo> menuInfosss)
        {
            foreach (var item in menuInfoList)
            {
                var children = menuInfosss.Where(x => x.ParentId == item.Id).Select(m => new BasicData_MenuInfoDTOs(m)).OrderBy(d => d.Sort).ToList();
                // 递归获取子菜单项的子菜单项
                GetChild(children, menuInfosss);
                item.Children = children.Count>0 ? children : null;
            }
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<object>> CreateBasicData_MenuInfoAsync(BasicData_MenuInfoDTO query)
        {
            //实体转换
            var basicData_MenuInfo = _mapper.Map<BasicData_MenuInfo>(query);
            basicData_MenuInfo.Id = Guid.NewGuid().ToString();
            //basicData_MenuInfo.MenuInfoNo = query.MenuInfoNo == null ? GenerateNumber() : query.MenuInfoNo;
            basicData_MenuInfo.Creator = _userServices.UserName;
            basicData_MenuInfo.Createtime = DateTime.Now;

            return _ibasicData_MenuInfoRepository.CreateBasicData_MenuInfoAsync(basicData_MenuInfo);
        }


        /// <summary>
        /// 批量删除菜单
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<object>> BatchDeletionBasicData_MenuInfoAsync(List<string>? ids)
        {
            if (ids.Count < 1)
            {
                throw new Exception("请先选择数据！");
            }
            return await _ibasicData_MenuInfoRepository.BatchDeletionBasicData_MenuInfoAsync(ids);
        }


        /// <summary>
        /// 更新菜单
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<object>> UpdateBasicData_MenuInfoAsync(BasicData_MenuInfoDTO query)
        {
            if (query == null)
            {
                throw new Exception("请先选择数据！");
            }

            BasicData_MenuInfo basicData_Menu = _mapper.Map<BasicData_MenuInfo>(query);
            basicData_Menu.Updator = _userServices.UserName;

            return await _ibasicData_MenuInfoRepository.UpdateBasicData_MenuInfoAsync(basicData_Menu);

        }

        /// <summary>
        /// 获取选择菜单列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<List<BasicData_MenuInfo>>> GetBasicData_MenuInfoSelectListAsync(QueryBasicData_MenuInfoDTO query)
        {
            var result = new OperationResult<List<BasicData_MenuInfo>>();

            result.Data = await GetEntityListAsync(m => m.MenuInfoName.Contains(query.MenuInfoName ?? string.Empty) && m.IsDel == 0);

            return result;
        }


        /// <summary>
        /// 根据id获取菜单列表
        /// </summary>
        /// <param name="menuInfoId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<BasicData_MenuInfoDTOs>> GetBasicData_MenuInfoByIdListAsync(List<string> menuInfoId)
        {
            //获取菜单集合
            var basicData_MenuInfos = await GetEntityListAsync(m => menuInfoId.Contains(m.Id) && m.IsDel == 0);

            //取出一级菜单
            List<BasicData_MenuInfoDTOs> menuInfoList = basicData_MenuInfos.Where(m => m.ParentId == null || m.ParentId == "").Select(m => new BasicData_MenuInfoDTOs(m)).OrderBy(m => m.Sort).ToList();

            GetChild(menuInfoList, basicData_MenuInfos);


            return menuInfoList;
        }
    }
}
